package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.dmsdbj.itoo.tool.constantutil.YearSemester;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.TeacherCourseEntity;
import com.tfjybj.itoo.teach.model.CourseModel;
import com.tfjybj.itoo.teach.model.StaffModel;
import com.tfjybj.itoo.teach.model.TeacherCourseAndCourseModel;
import com.tfjybj.itoo.teach.model.TeacherCourseModel;
import com.tfjybj.itoo.teach.provider.service.CourseService;
import com.tfjybj.itoo.teach.provider.service.StaffService;
import com.tfjybj.itoo.teach.provider.service.TeacherCourseService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * TeacherCourseController
 * teacherCourse表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"教师授课接口"})
@RequestMapping(value = "/teacherCourse")
@RestController
public class TeacherCourseController {

    @Resource
    private TeacherCourseService teacherCourseService;
    @Resource
    private CourseService courseService;
    @Resource
    private StaffService staffService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model TeacherCourseModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody TeacherCourseModel model) {
		if (StringUtils.isEmpty(model.getCourseId())){
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
		if (StringUtils.isEmpty(model.getTeacherId())){
            return ItooResult.build(ItooResult.FAIL, "teacherId为空");
        }
        TeacherCourseEntity teacherCourseEntity = new TeacherCourseEntity();
        BeanUtils.copyProperties(model, teacherCourseEntity);

        teacherCourseService.save(teacherCourseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        teacherCourseService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        teacherCourseService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改--(郄子硕修改--增加学年学期转换）
     *
     * @param model TeacherCourseModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改teacherCourse")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody TeacherCourseModel model) {
		if (StringUtils.isEmpty(model.getCourseId())){
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
		if (StringUtils.isEmpty(model.getTeacherId())){
            return ItooResult.build(ItooResult.FAIL, "teacherId为空");
        }
        TeacherCourseEntity teacherCourseEntity = new TeacherCourseEntity();
        BeanUtils.copyProperties(model, teacherCourseEntity);
        teacherCourseEntity.setSchoolYear(YearSemester.getYearSemesterCodeByName(teacherCourseEntity.getSchoolYear()));
        teacherCourseService.updateById(teacherCourseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找TeacherCourse
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        TeacherCourseEntity teacherCourseEntity = teacherCourseService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseEntity);
    }

    /**
     * 分页查询所有TeacherCourse
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有TeacherCourse")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<TeacherCourseEntity> teacherCourses = teacherCourseService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourses);
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据课程_id模糊查询teacherCourse
     *
     * @param courseId  课程_id
     * @return 模糊查询的teacherCourse
     * @author 郄子硕
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据课程_id模糊查询teacherCourse", notes = "分页根据课程_id模糊查询teacherCourse")
    @GetMapping(value = "queryTeacherCourseByLikeCourseId")
    public ItooResult queryTeacherCourseByLikeCourseId(@RequestParam(required = false, defaultValue = "") String courseId) {
        List<TeacherCourseModel> teacherCourseList = teacherCourseService.queryByLikeCourseId(courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseList);
    }

    /**
     * 根据课程id查询teacherCourse
     *
     * @param courseId  课程id
     * @return 查询的teacherCourse
     * @author 郄子硕
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据课程id查询teacherCourse授课信息", notes = "根据课程id查询teacherCourse授课信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "课程id", dataType = "String", required = true)
    })
    @GetMapping(value = "queryTeacherCourseListByTeacherId/{courseId}")
    public ItooResult queryTeacherCourseListByCourseId(@RequestParam(required = false, defaultValue = "") String courseId) {
        PageInfo<TeacherCourseEntity> teacherCourseList = teacherCourseService.queryTeacherCourseListByCourseId(courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseList);
    }

    /**
     * 查询SchoolYear
     *
     * @return 查询的SchoolYear
     * @author 郄子硕
     * @since 2.0.0 2018-11-20 15:00:22
     */
    @ApiOperation(value = "查询SchoolYear", notes = "查询SchoolYear")
    @GetMapping(value = "querySchoolYear")
    public ItooResult querySchoolYear() {
        List<String> list = YearSemester.getYearSemesterName(-1,1);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", list);
    }


    /**
     * 根据课程id和课程类型查询teacherCourse表和Course表
     *
     * @param teacherId  教师Id
     * @return TeacherCourseAndCourseModel
     * @author 郄子硕
     * @since 2.0.0 2018-11-10 20:52:30
     */
    @ApiOperation(value = "根据教师id和课程类型查询teacherCourse表", notes = "根据教师id和课程类型查询teacherCourse表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId", value = "教师id", dataType = "String", required = true)
    })
    @GetMapping(value = "queryTeacherCourseAndCourse")
    public ItooResult queryTeacherCourseAndCourse(@RequestParam(required = false, defaultValue = "") String teacherId) {
        PageInfo<TeacherCourseAndCourseModel> teacherCourseList = teacherCourseService.queryTeacherCourseAndCourse(teacherId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseList);
    }


    /**
     * 根据课程id集 查询授课教师集和
     *
     * @param ids ids
     * @return ItooResult 教师授课表List
     * @author 郄子硕
     * @since 2.0.0 2018-11-12 13:48:22
     */
    @ApiOperation(value = "根据课程id集 查询授课教师集和")
    @PostMapping(value = {"/queryTeacherCourseByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)

    public ItooResult queryTeacherCourseByIds(@RequestBody List<String> ids) {
        PageInfo<TeacherCourseModel> teacherCourseList = teacherCourseService.queryTeacherCourseByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功",teacherCourseList);
    }


    /**
     * 查询全部授课信息（分页，带code）
     *
     * @param pageNo  当前页
     * @param pageSize 每页条数
     * @return 授课信息
     * @author 郄子硕
     * @since 2.0.0 2018-11-16 09:49:52
     */
    @ApiOperation(value = "分页查询所有TeacherCourse(带code)")
    @GetMapping(value = {"/queryAllTeacherCourse/{pageNo}/{pageSize}"})
    public ItooResult queryAllTeacherCourse(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                            @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<TeacherCourseModel> teacherCourses = teacherCourseService.queryAllTeacherCourse(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourses);
    }

    /**
     * 多条件模糊查询teacherCourse
     *
     * @param schoolYear  学年学期
     * @param courseName  课程名称
     * @param teacherName 教师姓名
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的student
     * @author 郄子硕
     * @since 2.0.0 2018-12-12 19:18:38
     */
    @ApiOperation(value = "多条件模糊查询teacherCourse", notes = "多条件模糊查询teacherCourse")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolYear", value = "学年学期", dataType = "String"),
            @ApiImplicitParam(name = "courseName", value = "课程名称", dataType = "String"),
            @ApiImplicitParam(name = "teacherName", value = "教师姓名", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryStudentByLikeNameAndCode/{pageNo}/{pageSize}")
    public ItooResult queryStudentByLikeNameAndCode(@RequestParam(required = false, defaultValue = " ") String schoolYear,
                                                    @RequestParam(required = false, defaultValue = " ") String courseName,
                                                    @RequestParam(required = false, defaultValue = " ") String teacherName,
                                                    @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<TeacherCourseModel> studentList = teacherCourseService.queryListByTeacherAndCourse(courseName,teacherName,schoolYear, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentList);
    }

    /**
     * 根据关键词模糊查询课程名称或教师姓名
     *
     * @param strLike 课程名称或教师姓名关键词
     * @return 查询的teachercourse
     * @author 郄子硕
     * @since 2.0.0 2018-12-12 20:15:10
     */
    @ApiOperation(value = "分页根据关键词模糊查询课程名称或教师姓名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "strLike", value = "关键词", dataType = "String", required = true)
    })
    @GetMapping(value = "queryTeacherCourseByCodeAndNameStrLike/{pageNo}/{pageSize}")
    public ItooResult queryStudentInfoByCodeAndNameStrLike(
            @ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
            @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize,
            @RequestParam(required = false, defaultValue = "") String strLike) {
        PageInfo<TeacherCourseModel> teacherCourseList = teacherCourseService.queryTeacherCourseByCodeAndNameStrLike(pageNo, pageSize,strLike);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseList);
    }

    /**
     * 根据学年学期、教师_id和课程_id 查询TeacherCourse表
     *
     * @param teacherId  教师Id
     * @param courseId  课程Id
     * @param schoolYear  学年学期
     * @return TeacherCourseModel
     * @author 郄子硕
     * @since 2.0.0 2018-11-28 15:45:04
     */
    @ApiOperation(value = "根据学年学期、教师id和课程id查询教师授课信息")
    @GetMapping(value = {"/queryByTeacherIdAndCourseIdAndYear/{teacherId}/{courseId}/{schoolYear}"})
    public ItooResult queryAllTeacherCourse(@RequestParam(defaultValue = "") String teacherId,
                                            @RequestParam(defaultValue = "") String courseId,
                                            @RequestParam(defaultValue = "") String schoolYear) {
        PageInfo<TeacherCourseModel> teacherCourses = teacherCourseService.queryByTeacherIdAndCourseIdAndYear(teacherId, courseId,schoolYear);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourses);
    }

    /**
     * 根据学院ID查询本学院课程+公选课（调CourseService接口）
     *
     * @param academy 学院ID
     * @return coursemodel
     * @author 郄子硕
     * @since 2.0.0 2018-11-17 14:32:57
     */
    @ApiOperation(value = "根据学院ID查询本学院课程+公选课", notes = "根据学院ID查询本学院课程+公选课")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "academy", value = "学院ID", dataType = "String", required = true),
    })
    @GetMapping(value = "queryCourseByacademy")
    public ItooResult queryCourseByacademy(@RequestParam(defaultValue = "") String academy)
    {
        List<CourseModel> teacherCourseList = courseService.queryCourseByacademy(academy);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseList);
    }

    /**
     * 根据学院ID查询本学院教师（调StaffService接口）
     *
     * @param academy 学院ID
     * @return staffmodel
     * @author 郄子硕
     * @since 2.0.0 2018-11-19 11:19:33
     */
    @ApiOperation(value = "根据学院ID查询本学院教师", notes = "根据学院ID查询本学院教师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "academy", value = "学院ID", dataType = "String", required = true),
    })
    @GetMapping(value = "queryStaffByAcademy")
    public ItooResult queryStaffByAcademy(@RequestParam(defaultValue = "") String academy)
    {
        List<StaffModel> StaffCourseList = staffService.queryStaffByAcademy(academy);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", StaffCourseList);
    }

    /**
     * 添加（一键组合）
     *
     * @param model TeacherCourseModel
     * @return 添加结果
     * @author 郄子硕
     * @since 2.0.0 2018-11-19 09:59:46
     */
    @ApiOperation(value = "添加（一键组合）")
    @PostMapping(value = {"/addList"})
    public ItooResult addList(@RequestBody TeacherCourseModel model) {

//        teacherCourseService.addList(model);
//        return ItooResult.build(ItooResult.SUCCESS, "添加成功");

        if (teacherCourseService.addList(model)) {
            return ItooResult.build(ItooResult.SUCCESS, "添加成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "添加失败，部分教师授课信息已存在，请检查后添加！");
        }

    }

    /**
     * 导出
     *
     * @return studentmodel
     * @author 郄子硕
     * @since 2018-11-27 11:37:10
     */
    @ApiOperation(value = "导出")
    @GetMapping(value = "exportFile/exportStudentInfo")
    public ItooResult exportStaffInfo(HttpServletResponse response){
        return teacherCourseService.exportTeacherCourseInfo(response);
    }

    /**
     * 下载教师模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 郄子硕
     * @since 2.0.0 2018-11-27 20:43:28
     */
    @ApiOperation(value = "下载教师授课模板，批量导入使用", notes = "下载教师授课模板，批量导入使用")
    @GetMapping(value = {"/downLoadTemplate"})
    public ItooResult downTemplate(HttpServletResponse response) {
        if (teacherCourseService.downTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载教师授课模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载教师授课模板失败");
        }
    }

    /**
     * 批量导入教师授课信息
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 郄子硕
     * @since 1.0.0 2018-11-28 15:08:51
     */
    @ApiOperation(value = "批量导入教师授课信息")
    @PostMapping(value = {"/importTeacherCourse"})
    public ItooResult importTeacherCourse(@RequestParam(value = "file") MultipartFile multipartFile,
                                          HttpServletRequest request, HttpServletResponse response) {
        return teacherCourseService.importTeacherCourse(multipartFile, request, response);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return 教师授课信息model
     * @author 郄子硕
     * @since 2018-12-2 12:47:30
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (teacherCourseService.downErrorList(errorListId,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }
    }
}    
